﻿/*******************************************************************************
#                    ADMArticleBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-21 13:08:14
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
using IBatisNet.Extend;
using IBatisNet.Common.Utilities;
#endregion

namespace ExpertMIS.Brokers
{
    public class ADMArticleBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateADMArticleEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateADMArticleEntity", entity);
        }

        private object InsertADMArticleEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertADMArticleEntity", entity);
        }

        private object DeleteADMArticleEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteADMArticleEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectADMArticleEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                InsertADMArticleEntity(entity);
                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateADMArticleEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            ADMArticleEntity entity = new ADMArticleEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteADMArticleEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Methods

        public IList<ADMArticleEntity> GetArticlesByColumn(string ColumnOID, int topicCount)
        {
            if (string.IsNullOrEmpty(ColumnOID))
            {
                return null;
            }
            string strTop = "*";
            if (topicCount > 0)
            {
                strTop = string.Format(" Top {0} * ", topicCount);
            }

            Hashtable hsQuery = new Hashtable();
            hsQuery.Add("ColumnOID", ColumnOID);
            hsQuery.Add("Count", strTop);

            return SqlMap.QueryForList<ADMArticleEntity>("SelectArticlesByColumns", hsQuery);
        }

        public DataTable GetArticlesByColumn(string ColumnOID, PagingSize PagingSize)
        {
            string sql = "SELECT * FROM T_ADM_Article WHERE F_ColumnID = '{0}'";
            sql = string.Format(sql, ColumnOID);

            return SYS.Common.Broker.SQLBuilderBroker.GetSQLBuilderBroker().GetQueryView(sql, PagingSize);
        }

        /// <summary>
        /// 判断是否为最新文章
        /// </summary>
        /// <returns></returns>
        public bool IsNewArticle(ADMArticleEntity entity)
        {
            if (entity == null || string.IsNullOrEmpty(entity.OID))
            {
                return false;
            }

            // 读取环境变量最新文章最小时间值
            SYS.Common.Broker.EnvironmentBroker broker = new SYS.Common.Broker.EnvironmentBroker();
            SYS.Common.Broker.EnvironmentEntity envEntity = broker.GetEnvironmentValue("NewArticleLimitDate", false);

            if (envEntity == null)
            {
                return false;
            }

            int defaultDays = 10;
            int.TryParse(envEntity.Value,out defaultDays);

            if (entity.PublishedTime > DateTime.Now.AddDays(defaultDays))
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 判断是否为最新文章
        /// </summary>
        /// <returns></returns>
        public bool IsHotArticle(ADMArticleEntity entity)
        {
            if (entity == null || string.IsNullOrEmpty(entity.OID))
            {
                return false;
            }

            // 读取环境变量最新文章排名情况
            SYS.Common.Broker.EnvironmentBroker broker = new SYS.Common.Broker.EnvironmentBroker();
            SYS.Common.Broker.EnvironmentEntity envEntity = broker.GetEnvironmentValue("HotArticleTopN", false);

            if (envEntity == null)
            {
                return false;
            }

            Hashtable hsQuery = new Hashtable();
            hsQuery.Add("OID", entity.OID);
            hsQuery.Add("TopN",string.Format(" Top {0} * ", envEntity.Value));
            return (int)SqlMap.QueryForObject("IsHotArticle",hsQuery) > 0;
        }

        /// <summary>
        /// 更新文章浏览次数
        /// </summary>
        /// <param name="OID">文章ID</param>
        /// <param name="nCount">浏览次数</param>
        /// <returns></returns>
        public bool UpdateArticleBrowserTimes(string OID, int nCount)
        {
            try
            {
                if (string.IsNullOrEmpty(OID) || nCount < 0)
                {
                    return false;
                }

                ADMArticleEntity entity = GetArticleByOID(OID);
                if (entity == null)
                {
                    return false;
                }
                else
                {
                    entity.EntityState = EntityState.Old;
                }

                entity.BrowseTimes += nCount;
                SaveEntity(entity, null);
                return true;
            }
            catch
            {
                return false;
            }

        }

        public ADMArticleEntity GetArticleByOID(string OID)
        {
            Hashtable hsQuery = new Hashtable();
            hsQuery.Add("OID", OID);
            return GetEntity(hsQuery) as ADMArticleEntity;
        }

        #endregion
    }
}